package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.*;
import cn.wolfcode.crm.query.BuyrecordObject;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IBuyrecordService;
import cn.wolfcode.crm.service.ICustomerService;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/buyrecord")
public class BuyrecordController {
    @Autowired
    private IBuyrecordService buyrecordService;
@Autowired
private IEmployeeService employeeService;
@Autowired
private ICustomerService customerService;
@Autowired
private ISystemDictionaryItemService systemDictionaryItemService;
    @RequestMapping("/list")
    public String list(Model model, BuyrecordObject qo) {
        PageInfo result = buyrecordService.query(qo);
//        List<SystemDictionaryItem> jobs = systemDictionaryItemService.queryBysystemDictionarySn("job");
//        List<Employee> sellers = employeeService.listByRoleSn(new String[]{Role.ROLE_SN_MARKET_MANAGER, Role.ROLE_SN_MARKET});
//        List<SystemDictionaryItem> sellers=  systemDictionaryItemService.selectMy();
        List<Customer> customers = customerService.list();
        model.addAttribute("customers",customers);
        List<SystemDictionaryItem> kcs = systemDictionaryItemService.queryBysystemDictionarySn("kc");
        model.addAttribute("kcs", kcs);
        model.addAttribute("pageInfo", result);
        model.addAttribute("qo", qo);
        return "buyrecord/list";
    }

    @RequestMapping("/input")
    public String input(Model model, Long id) {
        if (id != null) {
            model.addAttribute("buyrecord", buyrecordService.get(id));
        }
        return "buyrecord/input";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Model model, Long id) {
        try {
            buyrecordService.delete(id);
        } catch (Exception e) {
            return new JsonResult(false, "操作失败");
        }
        return new JsonResult(true, "操作成功");
    }


    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult save(Buyrecord buyrecord) {
        try {
            if (buyrecord.getId() != null) {
                buyrecordService.update(buyrecord);
            } else {
                buyrecordService.save(buyrecord);
            }
        } catch (Exception e) {
            return new JsonResult(false, "操作失败");
        }
        return new JsonResult(true, "操作成功");
    }

}
